<?php

/**
 * This is the model class for table "admin".
 *
 * The followings are the available columns in table 'admin':
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $email
 * @property string $lastname
 * @property string $firstname
 * @property string $about
 * @property string $id_profile
 * @property integer $del_flg
 */
class Admin extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Admin the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'admin';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, password, email, id_profile', 'required'),
            array('email', 'email'),
            array('id_profile, del_flg', 'numerical', 'integerOnly'=>true),
			array('username', 'length', 'max'=>20),
			array('password, email', 'length', 'max'=>128),
			array('lastname, firstname', 'length', 'max'=>50),
			array('about', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username, password, email, lastname, firstname, about', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '#',
			'username' => Yii::t('app', 'Username'),
			'password' => Yii::t('app', 'Password'),
			'email' => Yii::t('app', 'Email'),
			'lastname' => Yii::t('app', 'Lastname'),
			'firstname' => Yii::t('app', 'Firstname'),
			'about' => Yii::t('app', 'About'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($roles)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
        $criteria->addCondition('del_flg = 0');
        $criteria->addCondition('id_profile = :id_profile');
        $criteria->params = array(':id_profile' => $roles);
		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('lastname',$this->lastname,true);
		$criteria->compare('firstname',$this->firstname,true);
		$criteria->compare('about',$this->about,true);

		return new CActiveDataProvider($this, array(
            'pagination'=>array(
                'pageSize'=>SystemConfig::PAGE_SIZE,
            ),
			'criteria'=>$criteria,
		));
	}

    public function beforeSave() {
        $this->password = self::encrypt($this->password);
        return parent::beforeSave();
    }

    /**
     * @param string $string
     * @return string md5
     */
    public static function encrypt($string="")
    {
        return md5($string);
    }

    public static function getObjAnalyster(){
        $criteria=new CDbCriteria;
        $criteria->addCondition('id_profile = :id_profile');
        $criteria->addCondition('del_flg = 0');
        $criteria->params = array(':id_profile' => Constant::ROLE_ANALYST);
        $dataObj = self::model()->findAll($criteria);
        if($dataObj){
            return $dataObj;
        }else{
            return false;
        }
    }

    public static function getListAnalyster(){
        $dataObj = self::getObjAnalyster();
        $listAnalyst = array();
        if($dataObj){
            foreach($dataObj as $user){
                $listAnalyst[$user->id] = $user->username.'  ['.$user->firstname.' '.$user->lastname.']';

            }
        }
        return $listAnalyst;
    }

    public static function getAnalysterDetail($id){
        $criteria=new CDbCriteria;
        $criteria->addCondition('id_profile = :id_profile');
        $criteria->addCondition('del_flg = 0');
        $criteria->params = array(':id_profile' => Constant::ROLE_ANALYST);
        return self::model()->findByPk($id, $criteria);
    }
}